import React from 'react'
import PureRenderMixin from 'react-addons-pure-render-mixin'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { hashHistory } from 'react-router'
import BuyAndStore from '../../../components/BuyAndStore'
import * as storeActionsFromFile from '../../../actions/store.js'

class Buy extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
        this.state={
            isStore:false
        }
    }
    render() {
        return (
            <div>
                <BuyAndStore isStore={this.state.isStore} buyHandle={this.buyHandle.bind(this)} storeHandle={this.storeHandle.bind(this)}/>
            </div>
        )
    }
    componentDidMount(){
        console.log(123,this.props.store);//测试redux返回数据
        console.log(456,this.props.storeActions);
        console.log(789,this.props.userinfo);
    }
    //购买事件
    buyHandle(){
        //验证登录
        const loginFlag=this.loginCheck();
        if(!loginFlag){
            return
        }

        //购买的流程
        //...
        //...
        //...
        //跳转到用户主页
        hashHistory.push('/User')
    }
    //收藏事件
    storeHandle(){
        //验证登录
        const loginFlag=this.loginCheck();
        if(!loginFlag){
            return
        }

        //
    }
    //验证登录
    loginCheck(){
        const id=this.props.id;
        const userinfo=this.props.userinfo;
        if(!userinfo.username){
            //跳转到登录页面
            hashHistory.push('/login/' + encodeURIComponent('/detail/' + id));
            return false
        }
        return true
    }
}

// 使用 require.ensure 异步加载，还不支持 ES6 的 export
//export default Buy
// -------------------redux react 绑定--------------------
function mapStateToProps(state) {
    return {
        userinfo: state.userinfo,
        store:state.store
    }
}

function mapDispatchToProps(dispatch) {
    return {
        storeActions: bindActionCreators(storeActionsFromFile, dispatch)
    }
}
export default connect(
    mapStateToProps,
    mapDispatchToProps
)(Buy)